package net.uiiang.android.alkaid.object;

import net.uiiang.android.alkaid.Constant;
import net.uiiang.android.alkaid.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;

public class FileView {

    private TextView title;

    private ImageView icon;
    
    private ImageView favorite;

    private TextView desc;

    // private CheckBox checkBox;
    private LayoutInflater mInflater;

    private FileItem mFileItem;

    private CheckedTextView checkedTextView;

    public View newFileView(Context context, FileItem fileItem) {
        return newFileView(context, fileItem, false);
    }

    public View newFileView(Context context, FileItem fileItem,
            boolean mutipleSelect) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View convertView = mInflater
                .inflate(R.layout.list_item_icon_text, null);

        mFileItem = fileItem;
        checkedTextView = ((CheckedTextView) convertView
                .findViewById(R.id.checkbox));
        title = ((TextView) convertView.findViewById(R.id.title));
        icon = ((ImageView) convertView.findViewById(R.id.icon));
        desc = ((TextView) convertView.findViewById(R.id.desc));
        favorite = ((ImageView) convertView.findViewById(R.id.favorite));

        title.setText(fileItem.getTitle());
        desc.setText(fileItem.getDesc());
        icon.setImageDrawable(fileItem.getIcon());
        
        if(fileItem.isFileFavorite()){
            favorite.setVisibility(View.VISIBLE);
        }else{
            favorite.setVisibility(View.INVISIBLE);
        }
        if ((!Constant.FILE_TYPE_DIRECTORY.equalsIgnoreCase(fileItem
                .getFileType()) && !Constant.FILE_TYPE_FILE
                .equalsIgnoreCase(fileItem.getFileType()))
                || !mutipleSelect) {
            checkedTextView.setVisibility(View.INVISIBLE);
        } else {
            checkedTextView.setChecked(fileItem.isChecked());
            checkedTextView.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    checkedTextView.setChecked(!getCheckBoxState());
                    mFileItem.setChecked(getCheckBoxState());
                }
            });
        }
        return convertView;
    }

    public void toggleCheckBoxState() {
        setCheckBoxState(!getCheckBoxState());
    }

    public void setCheckBoxState(boolean bool) {
        checkedTextView.setChecked(bool);
        mFileItem.setChecked(bool);
    }

    public boolean getCheckBoxState() {
        return checkedTextView.isChecked();
    }

    public TextView getTitle() {
        return title;
    }

    public TextView getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc.setText(desc);
    }

    public void setDesc(TextView desc) {
        this.desc = desc;
    }

    public void setTitle(String title) {
        this.title.setText(title);
    }

    public void setTitle(TextView title) {
        this.title = title;
    }

    public ImageView getIcon() {
        return icon;
    }

    public void setIcon(ImageView icon) {
        this.icon = icon;
    }

}
